#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define FILE_PATH "recv.mp3"
#define BUFF_SIZE 1024

int main()
{
    FILE* file = fopen(FILE_PATH, "wb");
    if (NULL == file)
    {
        printf("Open file %s error! Press any key to exit.", FILE_PATH);
        exit(0);
    }

    int sock = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in sock_addr;
    memset(&sock_addr, 0, sizeof(sock_addr));
    sock_addr.sin_family = AF_INET;
    sock_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    sock_addr.sin_port = htons(1234);
    connect(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr));

    char buffer[BUFF_SIZE] = {0};
    int recvCount;
    while ((recvCount = recv(sock, buffer, BUFF_SIZE, 0)) > 0)
    {
        fwrite(buffer, recvCount, 1, file);
    }
    printf("File reciece successed!\n");

    fclose(file);
    close(sock);

    return 0;
}